package railRoad.worker;

import railRoad.exception.ReaderException;

public class FactoryRW {
	static final private WorkerClass worker = WorkerClass.getInstance();

	static public void readJSON(String path) {
		// Sample read from JSON
		worker.initReader("JSON");
		try {
			// "C:\\Users\\Rukin\\workspace\\mvn_RailRoad\\RailRoad.json"
			worker.read(path);
		} catch (ReaderException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	static public void readXML(String path) {
		worker.initReader("XML");
		try {
			// "data\\RailRoad.xml"
			worker.read(path);
		} catch (ReaderException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	static public void writeXML(String path) {
		worker.initReader("XML");
		try {
			worker.write(path);
		} catch (ReaderException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	static public void initDB(String login, String password) {
		worker.initReader("H2");
		worker.initDB(login, password);
	}

	static public void closeDB() {
		worker.closeDB();
	}

	static public void readDB() {
		// Sample read from database
		try {
			worker.readDB();
		} catch (ReaderException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		// System.out.println(Schedule.getInstance().toString());
	}

	static public void writeDB() {
		try {
			worker.writeDB();
		} catch (ReaderException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	static public<T> void insert(T object, int parentId)
	{
		try
		{
			worker.insert(object, parentId);
		}
		catch (ReaderException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}//insert
	
	static public<T> void update(T object)
	{
		try
		{
			worker.update(object);
		}
		catch (ReaderException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}//update
	
	static public<T> void delete(T object)
	{
		try
		{
			worker.delete(object);
		}
		catch (ReaderException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}//delete
	
	static public<T> T select(T object, int id)
	{
		T obj = null;
		try
		{
		 	obj = worker.select(object, id);
		}
		catch (ReaderException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return obj;
		
	}//select int
	
	static public<T> T select(T object, String field)
	{
		T obj = null;
		
		try
		{
			obj = worker.select(object, field);
		}
		catch (ReaderException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
		return obj;
	}//select String
}
